package tsinghua.smxq.mc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class SelectCityActivity extends Activity {

	private Spinner province;
	private Spinner city;
	private Button ok;
	private Button backbtn;
	private Button cancel;
	private File f = new File("/data/data/tsinghua.smxq.mc/databases/data.db");
	private List<String> proset = new ArrayList<String>();
	private List<String> citset = new ArrayList<String>();
	private int pro_id;
	String cityname = "北京";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectcity);

		String dbDirPath = "/data/data/tsinghua.smxq.mc/databases";
		File dbDir = new File(dbDirPath);
		if (!dbDir.exists())
			dbDir.mkdir();
		InputStream is = this.getApplicationContext().getResources()
				.openRawResource(R.raw.db_weather);
		FileOutputStream os;
		try {
			os = new FileOutputStream(dbDirPath + "/data.db");
			byte[] buffer = new byte[1024];
			int count = 0;
			while ((count = is.read(buffer)) > 0) {
				os.write(buffer, 0, count);
			}
			is.close();
			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ok = (Button) findViewById(R.id.buttonOK);
		cancel = (Button) findViewById(R.id.buttonCancel);
		backbtn = (Button) findViewById(R.id.backBtn);
		province = (Spinner) findViewById(R.id.provinces);
		ArrayAdapter<String> pro_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getProSet());
		province.setAdapter(pro_adapter);
		province.setOnItemSelectedListener(new SelectProvince());
		backbtn.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SelectCityActivity.this.finish();
			}

		});

		city = (Spinner) findViewById(R.id.city);
		city.setOnItemSelectedListener(new SelectCity());
		OnClickListener listener = new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("Result", cityname);
				intent.putExtras(bundle);
				SelectCityActivity.this.setResult(RESULT_OK, intent);
				finish();

			}
		};

		ok.setOnClickListener(listener);
		listener = new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("Result", cityname);
				intent.putExtras(bundle);
				SelectCityActivity.this.setResult(RESULT_CANCELED, intent);
				finish();

			}
		};

		cancel.setOnClickListener(listener);

	}

	class SelectProvince implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			pro_id = position;
			city.setAdapter(getAdapter());

		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	}

	class SelectCity implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			cityname = parent.getItemAtPosition(position).toString();
			Toast.makeText(getApplicationContext(),
					cityname + " " + getCityNum(position), 2000).show();

		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	}

	public List<String> getProSet() {
		SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
		Cursor cursor = db1.query("provinces", null, null, null, null, null,
				null);
		while (cursor.moveToNext()) {
			String pro = cursor.getString(cursor.getColumnIndexOrThrow("name"));
			proset.add(pro);
		}
		cursor.close();
		db1.close();
		return proset;
	}

	public List<String> getCitSet(int pro_id) {
		citset.clear();
		SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
		Cursor cursor = db1.query("citys", null, "province_id=" + pro_id, null,
				null, null, null);
		while (cursor.moveToNext()) {
			String pro = cursor.getString(cursor.getColumnIndexOrThrow("name"));
			citset.add(pro);
		}
		cursor.close();
		db1.close();
		return citset;
	}

	public ArrayAdapter<String> getAdapter() {
		ArrayAdapter<String> adapter1 = new ArrayAdapter(this,
				android.R.layout.simple_spinner_item, getCitSet(pro_id));
		return adapter1;
	}

	public long getCityNum(int position) {
		SQLiteDatabase db1 = SQLiteDatabase.openOrCreateDatabase(f, null);
		Cursor cursor = db1.query("citys", null, "province_id=" + pro_id, null,
				null, null, null);
		cursor.moveToPosition(position);
		long citynum = Long.parseLong(cursor.getString(cursor
				.getColumnIndexOrThrow("city_num")));
		cursor.close();
		db1.close();
		return citynum;
	}

}
